/** 
  国家公派录取确认
*/
<template>
  <div class="admission-confirm-container">
    <div v-if="indexKey === 1">
      <condition-card @reset="reset" @search="onSubmit" label-position="right" label-width="150px">
        <el-row :gutter="24" style="height: 50px;">
          <el-col :span="8">
            <!-- 学院 -->
            <el-form-item
              :label="$t('externamExchangeBase.college')" 
              prop="studentFaculty">
              <el-select v-model="queryForm.condition.studentFaculty" filterable @change="changeCollege" :placeholder="$t('common.pla.pleaseSelect')" clearable style="width: 100%;">
                <el-option
                  v-for="item in options.studentFaculty"
                  :key="item.key"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <!-- 专业 -->
            <el-form-item
              :label="$t('externamExchangeBase.profession')" 
              prop="studentProfession">
              <el-select v-model="queryForm.condition.studentProfession" filterable :placeholder="$t('common.pla.pleaseSelect')" clearable style="width: 100%;">
                <el-option
                  v-for="item in options.studentProfession"
                  :key="item.key"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <!-- 国家 -->
            <el-form-item
              :label="$t('interdisciplinaryManagement.country')" 
              prop="projectNationCode">
              <el-select v-model="queryForm.condition.projectNationCode" @change="(value) => changeNation(value, 'search')" filterable :placeholder="$t('common.pla.pleaseSelect')" clearable style="width: 100%;">
                <el-option
                  v-for="item in options.nationId"
                  :key="item.key"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <!-- 合作大学 -->
            <el-form-item
              :label="$t('externamExchangeBase.cooperativeUniversity')" 
              prop="projectCollegeId">
              <el-select v-model="queryForm.condition.projectCollegeId" :placeholder="$t('common.pla.pleaseSelect')" clearable style="width: 100%;">
                <el-option
                  v-for="item in options.collegeId"
                  :key="item.id"
                  :label="item.collegeName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <!-- 项目开始时间 -->
            <el-form-item
              :label="$t('projectApplication.projectStartTime')" 
              prop="projectStartTime">
              <el-date-picker
                style="width: 100%"
                v-model="queryForm.condition.projectStartTimeStr"
                type="date"
                value-format="yyyy-MM-dd"
                :placeholder="$t('graduateBase.chooseDateAndTime')"
                :picker-options="xmkssjPickerOptions">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <!-- 项目结束时间 -->
            <el-form-item
              :label="$t('projectApplication.projectEndTime')" 
              prop="projectEndTime">
              <el-date-picker
                style="width: 100%"
                v-model="queryForm.condition.projectEndTimeStr"
                type="date"
                value-format="yyyy-MM-dd"
                :placeholder="$t('graduateBase.chooseDateAndTime')"
                :picker-options="xmjssjPickerOptions">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="8">
            <!-- 学号/姓名 -->
            <el-form-item
              :label="$t('exchangeProjectAudit.studentParam')" 
              prop="studentParam">
              <el-input :placeholder="$t('common.pla.pleaseInput')" v-model="queryForm.condition.studentParam" clearable :maxlength="32" style="width: 100%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <!-- 录取状态 -->
            <el-form-item
              :label="$t('nationGovernAdmission.admissionStatus')" 
              prop="participantPass">
              <el-select v-model="queryForm.condition.participantPass" :placeholder="$t('common.pla.pleaseSelect')" clearable style="width: 100%;">
                <el-option
                  v-for="item in options.participantPass"
                  :key="item.key"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </condition-card>
      <el-card shadow="never" v-loading="loading" body-style="padding: 24px;" class="card-margin">
        <h1 class="condition-title">{{$t('exchangeProjectAudit.auditTitle')}}</h1>
        <div class="edu-opertion-bar sub-title">
          <div class="float-right" style="display: flex;">
            <!-- 录取 -->
            <el-button v-if="permitToAdmission" type="primary" @click="admit()">{{ $t('nationGovernAdmission.admit') }}</el-button>
            <!-- 不录取 -->
            <el-button v-if="permitToNotAdmission" @click="notAdmit()">{{ $t('nationGovernAdmission.notAdmit') }}</el-button>
            <!-- 放弃 -->
            <el-button v-if="permitToGiveUp" @click="giveUp()">{{ $t('nationGovernAdmission.giveUp') }}</el-button>
            <!-- 导出 -->
            <el-button v-if="permitToExport" @click="exportInfo()">{{ $t('common.btn.export') }}</el-button>
          </div>
        </div>
        <edu-table :pagination="false" :order='true' :tooltipEffect="tooltipEffect" stripe :data="dataList" size="mini" @selection-change="handleSelectionChange">
          <edu-table-column :label="$t('externamExchangeBase.studentId')" prop="studentId"></edu-table-column>
          <edu-table-column :label="$t('externamExchangeBase.studentName')" prop="studentName"></edu-table-column>
          <edu-table-column :label="$t('externamExchangeBase.college')" prop="studentFacultyName"></edu-table-column>
          <edu-table-column :label="$t('externamExchangeBase.profession')" prop="studentProfessionName"></edu-table-column>
          <edu-table-column :label="$t('projectManage.projectSubCategory')" prop="projectSubCategoryName" width="100"></edu-table-column>
          <edu-table-column :label="$t('projectManage.projectName')" prop="projectMngmtName"></edu-table-column>
          <edu-table-column :label="$t('interdisciplinaryManagement.country')" prop="projectNationName">
            <template slot-scope="scope">
              <div v-if="scope.row.changes.length > 0">
                <span v-if="scope.row.changes[0].nationChanged" style="color: red;">{{ scope.row.changes[0].countryRegion }}</span>
                <span v-else>{{ scope.row.projectNationName }}</span>
              </div>
              <div v-else>{{ scope.row.projectNationName }}</div>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('externamExchangeBase.cooperativeUniversity')" prop="projectCollegeCn">
            <template slot-scope="scope">
              <div v-if="scope.row.changes.length > 0">
                <span v-if="scope.row.changes[0].collegeChanged" style="color: red;">{{ scope.row.changes[0].cooperateUniversity }}</span>
                <span v-else>{{ scope.row.projectCollegeCn }}</span>
              </div>
              <div v-else>{{ scope.row.projectCollegeCn }}</div>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('projectApplication.projectStartTime')" prop="projectStartTime" width="110">
            <template slot-scope="scope">
              <div v-if="scope.row.changes.length > 0">
                <span v-if="scope.row.changes[0].startTimeChanged" style="color: red;">{{ scope.row.changes[0].projectStartTime }}</span>
                <span v-else>{{ scope.row.projectStartTime }}</span>
              </div>
              <div v-else>{{ scope.row.projectStartTime }}</div>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('projectApplication.projectEndTime')" prop="projectEndTime" width="110">
            <template slot-scope="scope">
              <div v-if="scope.row.changes.length > 0">
                <span v-if="scope.row.changes[0].endTimeChanged" style="color: red;">{{ scope.row.changes[0].projectEndTime }}</span>
                <span v-else>{{ scope.row.projectEndTime }}</span>
              </div>
              <div v-else>{{ scope.row.projectEndTime }}</div>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('projectApplication.projectFundingPeriod')" prop="projectFundingPeriod" width="130">
            <template slot-scope="scope">
              <div v-if="scope.row.changes.length > 0">
                <span v-if="scope.row.changes[0].fundingPeriodChanged" style="color: red;">{{ scope.row.changes[0].projectFundingPeriod }}</span>
                <span v-else>{{ scope.row.projectFundingPeriod }}</span>
              </div>
              <div v-else>{{ scope.row.projectFundingPeriod }}</div>
            </template>
          </edu-table-column>
          <edu-table-column :label="$t('projectApplication.projectStudyAbroadContent')" prop="projectStudyAbroadContent"></edu-table-column>
          <edu-table-column :label="$t('nationGovernAdmission.admissionStatus')" prop="participantPass" width="135">
            <template slot-scope="scope">
              <el-tag :type="scope.row.participantPass | getType">{{ scope.row.participantPass | participantPass }}</el-tag>
            </template>   
          </edu-table-column>
          <edu-table-column :label="$t('common.text.operation')" prop="operation" fit width="120">
            <template slot-scope="scope">
              <!-- 异动 -->
              <el-button v-if="permitToChange" type="text" @click="change(scope.row)">{{ $t('nationGovernAdmission.change') }}</el-button>
              <!-- 查看申请 -->
              <el-button v-if="permitToSelectApply" type="text" @click="selectApply(scope.row)">{{ $t('nationGovernAdmission.selectApply') }}</el-button>
            </template>   
          </edu-table-column>
        </edu-table>
        <el-row type="flex" class="row-bg" justify="end">
          <el-col>
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryForm.pageNum_"
              :page-sizes="[20, 40, 80, 100]"
              :page-size="queryForm.pageSize_"
              layout="total, sizes, prev, pager, next, jumper"
              :total="queryForm.total_"
              style="float:right;margin-top:10px"
            ></el-pagination>
          </el-col>
        </el-row>
      </el-card>
    </div>
    <!-- 查看出国申请 -->
    <div v-if="indexKey === 2">
      <audit-go-abroad-for-select :applyId="applyId" @change-project-index="changeProjectIndex"></audit-go-abroad-for-select>
    </div>
    <div v-if="dialogVisible">
      <el-dialog
        :before-close="beforeClose"
        :title="$t('nationGovernAdmission.change')"
        width="50%"
        ref="dialogForm"
        :visible.sync="dialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false">
        <el-form :model="dialogForm" label-position="right" ref="dialogForm" label-width="130px">
          <el-row>
            <el-col :span="24">
              <!-- 项目子类别 -->
              <el-form-item
                :label="$t('projectManage.projectSubCategory') + '：'"
                prop="projectSubCategoryName"
                :rules="[{ required: true, message: $t('projectManage.requireProjectSubCategory'), trigger: 'change' }]">
                <el-input v-model="dialogForm.projectSubCategoryName" disabled style="width: 100%">
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <!-- 项目名称 -->
              <el-form-item 
                :label="$t('projectManage.projectName') + '：'"
                prop="projectMngmtName"
                :rules="[{ required: true, message: $t('projectManage.requireProjectName'), trigger: 'blur'}]">
                <el-input v-model="dialogForm.projectMngmtName" disabled style="width: 100%"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <!-- 国家/地区 -->
              <el-form-item
                :label="$t('exchangeProjectAudit.projectNationCode')" 
                prop="nationId"
                :rules="[{ required: true, message: $t('nationGovernAdmission.requireProjectNationCode'), trigger: 'blur'}]">
                <el-select v-model="dialogForm.nationId" filterable @change="(value) => changeNation(value, 'dialogForm')" :placeholder="$t('common.pla.pleaseSelect')" clearable style="width: 100%;">
                  <el-option
                    v-for="item in options.nationId"
                    :key="item.key"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <!-- 合作大学 -->
              <el-form-item
                :label="$t('externamExchangeBase.cooperativeUniversity')" 
                prop="collegeId"
                :rules="[{ required: true, message: $t('nationGovernAdmission.requireCooperativeUniversity'), trigger: 'blur'}]">
                <el-select v-model="dialogForm.collegeId" :placeholder="$t('common.pla.pleaseSelect')" clearable style="width: 100%;">
                  <el-option
                    v-for="item in options.dialogCollegeId"
                    :key="item.id"
                    :label="item.collegeName"
                    :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <!-- 留学内容 -->
              <el-form-item 
                :label="$t('projectManage.studyAbroadContentName') + '：'"
                prop="projectStudyAbroadContent"
                :rules="[{ required: true, message: $t('projectManage.requireStudyAbroadContent'), trigger: 'blur'}]">
                <el-input v-model="dialogForm.projectStudyAbroadContent" disabled style="width: 100%"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <!-- 项目开始时间 -->
              <el-form-item
                :label="$t('projectApplication.projectStartTime')" 
                prop="projectStartTime"
                :rules="[{ required: true, message: $t('nationGovernAdmission.requireProjectStartTime'), trigger: 'blur'}]">
                <el-date-picker
                  style="width: 100%"
                  v-model="dialogForm.projectStartTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  :placeholder="$t('graduateBase.chooseDateAndTime')"
                  :picker-options="ydkssjPickerOptions">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <!-- 项目结束时间 -->
              <el-form-item
                :label="$t('projectApplication.projectEndTime')" 
                prop="projectEndTime"
                :rules="[{ required: true, message: $t('nationGovernAdmission.requireProjectEndTime'), trigger: 'blur'}]">
                <el-date-picker
                  style="width: 100%"
                  v-model="dialogForm.projectEndTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  :placeholder="$t('graduateBase.chooseDateAndTime')"
                  :picker-options="ydjssjPickerOptions">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <!-- 申请资助期限 -->
              <el-form-item :label="$t('projectApplication.projectFundingPeriod') + '：'" prop="projectFundingPeriod" :rules="[{ required: true, message: $t('projectApplication.requireProjectFundingPeriod'), trigger: 'blur'},{ validator: checkSpecial, trigger: 'blur'},{ validator: checkPositiveInteger, trigger: 'blur'} ]">
                <el-input
                  :maxlength="9"
                  :placeholder="$t('common.pla.pleaseInput')"
                  v-model="dialogForm.projectFundingPeriod"
                  clearable
                  style="width: 100%;">
                </el-input>
              </el-form-item>
            </el-col >
          </el-row>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <template>
            <el-button @click="cancel()">{{ $t('common.btn.cancel') }}</el-button>
            <el-button type="primary" @click="confirm('dialogForm')">{{ $t('common.btn.confirm') }}</el-button>
          </template>
        </span>
      </el-dialog>
    </div>

    <edu-export-dialog
      :disabled="exportButton"
      :title="$t('message.prompt')"
      ref="downloadRef"
      :data="exportResult">
    </edu-export-dialog>
  </div>
</template>
<script>
import UTILS from "common/src/utils/utils";
import { queryDic } from "common/src/api/dictionary";
import externalExchangeApi from 'common/src/api/externalExchange/externalExchange';
import auditGoAbroadForSelect from './auditGoAbroadForSelect';
export default {
  components: {
    'audit-go-abroad-for-select': auditGoAbroadForSelect,
  },
  data () {
    return {
      auths: [],
      userInfo: {},
      indexKey: 1,
      permitToAdmission: true,
      permitToNotAdmission: true,
      permitToChange: true,
      permitToGiveUp: true,
      permitToExport: true,
      permitToSelectApply: true,
      multipleSelection: [],
      queryForm: {
        pageNum_: 1,
        pageSize_: 20,
        total_: 0,
        condition: {
          studentFaculty: '',
          studentProfession: '',
          projectNationCode: '',
          projectCollegeId: '',
          projectStartTimeStr: '',
          projectEndTimeStr: '',
          studentParam: '',
          participantPass: ''
        }
      },
      options: {
        // 学院
        studentFaculty: [],
        // 专业
        studentProfession: [],
        // 国家
        nationId: [],
        // 合作大学
        collegeId: [],
        dialogCollegeId: [],
        // 录取状态
        participantPass: [
          {
            key: 1,
            label: '录取',
            value: 1
          },
          {
            key: 2,
            label: '未录取',
            value: 2
          },
          {
            key: 3,
            label: '放弃',
            value: 3
          }
        ]
      },
      dialogMode: 'add',
      dataList: [],
      exportButton: false,
      exportResult: null,
      applyId: '',
      dialogVisible: false,
      dialogForm: {
        projectSubCategoryName: '',
        projectMngmtName: '',
        nationId: '',
        collegeId: '',
        projectStudyAbroadContent: '',
        projectStartTime: '',
        projectEndTime: '',
        projectFundingPeriod: ''
      }
    }
  },
  filters: {
    getType(val) {
      switch(val) {
        case 0:
          return 'info'
        case 1:
          return 'success'
        case 2:
          return 'info'
        case 3:
          return 'info'
        default:
          return val
      }
    },
    participantPass(val) {
      switch(val) {
        case 0:
          return '未评'
        case 1:
          return '已录取'
        case 2:
          return '未录取'
        case 3:
          return '放弃'
        default:
          return val
      }
    }
  },
  computed: {
    // 项目开始时间
    xmkssjPickerOptions() {
      let _this = this;
      return {
        disabledDate: (value) => {
          if (_this.queryForm.condition.projectEndTimeStr) {
            if (value.getTime() > new Date(_this.queryForm.condition.projectEndTimeStr).getTime()) {
              return true;
            }
          }
          return false;
        }
      };
    },
    // 项目结束时间
    xmjssjPickerOptions() {
      let _this = this;
      return {
        disabledDate: (value) => {
          if (_this.queryForm.condition.projectStartTimeStr) {
            if (value.getTime() + (24 * 60 * 60 * 1000) < new Date(_this.queryForm.condition.projectStartTimeStr).getTime()) {
              return true;
            }
          }
          return false;
        }
      };
    },
    // 异动设置项目开始时间
    ydkssjPickerOptions() {
      let _this = this;
      return {
        disabledDate: (value) => {
          if (_this.dialogForm.projectEndTime) {
            if (value.getTime() > new Date(_this.dialogForm.projectEndTime).getTime()) {
              return true;
            }
          }
          return false;
        }
      };
    },
    // 异动设置项目结束时间
    ydjssjPickerOptions() {
      let _this = this;
      return {
        disabledDate: (value) => {
          if (_this.dialogForm.projectStartTime) {
            if (value.getTime() + (24 * 60 * 60 * 1000) < new Date(_this.dialogForm.projectStartTime).getTime()) {
              return true;
            }
          }
          return false;
        }
      };
    },
  },
  async created() {
    // 获取当前用户auths权限
    if (localStorage.getItem("sessiondata")) {
      if (JSON.parse(localStorage.getItem("sessiondata")).auths) {
        this.auths = JSON.parse(localStorage.getItem("sessiondata")).auths;
      }
      if (localStorage.getItem("user")) {
        this.userInfo = JSON.parse(localStorage.getItem("user"))
      }
    }
    this.initOptions()
    this.onSubmit()
  },
  methods: {
    async initOptions() {
      // 筛选条件字典查询
      let data = {
        lang: this.$store.getters.language || "cn",
        type: "allChild",
        keys: [
          "X_YX", // 学院
          "G_GJQU" // 国家
        ]
      };
      await queryDic(data).then(res => {
        UTILS.fillSelect(
          {
            studentFaculty: "X_YX", // 学院
            nationId: "G_GJQU", // 国家
          },
          res.data,
          this.options
        );
      });
    },
    // 校验特殊字符$
    checkSpecial(rule, value, callback) {
      let regEn = /[`$]/im;
      if (regEn.test(value)) {
        callback(new Error(this.$t('graduateBase.specialText')))
      } else {
        callback();
      }
    },
    // 校验申请期限
    checkPositiveInteger(rule, value, callback) {
      let regExp = /^[1-9]\d*$/;
      if (!regExp.test(value)) {
        callback(new Error(this.$t('externamExchangeBase.validatePositiveInteger')))
      } else {
        callback();
      }
    },
    reset() {
      Object.keys(this.queryForm.condition).forEach(key => this.queryForm.condition[key] = '');
      this.options.studentProfession = []
      this.options.collegeId = []
      this.onSubmit();
    },
    getListData() {
      externalExchangeApi.lastConfirmList(this.queryForm).then((res) => {
        this.dataList = res.data.list;
        this.queryForm.total_ = res.data.total_;
      })
    },
    onSubmit() {
      this.queryForm.pageNum_ = 1;
      this.getListData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.queryForm.pageSize_ = val;
      this.getListData();
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum_ = val;
      this.getListData();
    },
    // 回到列表页
    changeProjectIndex() {
      this.indexKey = 1
      this.multipleSelection = []
      this.onSubmit()
    },
    // 修改学院
    changeCollege(val) {
      this.options.studentProfession = [];
      this.queryForm.condition.studentProfession = ''
      let data = {
        lang: this.$store.getters.language || "cn",
        type: "allChild",
        keys: ["G_ZY"],
        filter: {
          prefix: [val],
          specilaCode: "XY2ZY",
          facultys: [val]
        }
      };
      queryDic(data).then(res => {
        UTILS.fillSelect(
          {
            studentProfession: "G_ZY" // 专业
          },
          res.data,
          this.options
        )
      })
    },
    // 修改国家
    async changeNation(val, type) {
      if (type === 'search') {
        this.queryForm.condition.projectCollegeId = ''
        this.options.collegeId = []
      } else if (type === 'dialogForm') {
        this.dialogForm.collegeId = ''
        this.options.dialogCollegeId = []
      } else if (type === 'change') {
        this.options.dialogCollegeId = []
      }
      // 获取合作大学
      if (val) {
        const resp = await externalExchangeApi.getCollegeByNationCode({
          "code": val
        })
        if (resp.code === 200) {
          if (type === 'search') {
            this.options.collegeId = resp.data
          } else if (type === 'dialogForm' || type === 'change') {
            this.options.dialogCollegeId = resp.data
          }
        }
      }
      if (!val) {
        if (type === 'search') {
          this.options.collegeId = []
        } else {
          this.options.dialogCollegeId = []
        }
      }
    },
    // 录取
    admit() {
      // 未勾选数据
      if (this.multipleSelection.length === 0) {
        this.$message.error(this.$t('externamExchangeBase.pleaseChoose'))
        return
      }
      // 勾选一条数据
      if (this.multipleSelection.length === 1) {
        // 录取状态为“已录取/未录取/放弃”--“请选择未评的数据”
        if (this.multipleSelection[0].participantPass !== 0) {
          this.$message.error(this.$t('externamExchangeBase.pleaseChooseNotComment'))
          return
        }
      }
      // 勾选多条数据
      if (this.multipleSelection.length > 1) {
        let notRated = false // 非未评
        for (let i = 0; i < this.multipleSelection.length; i++) {
          if (this.multipleSelection[i].participantPass !== 0) {
            notRated = true
          }
        }
        if (notRated) {
          this.$message.error(this.$t('externamExchangeBase.pleaseChooseNotComment'))
          return
        }
      }
      externalExchangeApi.lastListConfirm({
        "applyIdList": this.multipleSelection.map(value => {
          return value.applyId
        }),
        "participantPass": 1
      }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t('externamExchangeBase.admissionSuccessfully'))
          this.onSubmit()
        }
      })
    },
    // 不录取
    notAdmit() {
      // 未勾选数据
      if (this.multipleSelection.length === 0) {
        this.$message.error(this.$t('externamExchangeBase.pleaseChoose'))
        return
      }
      // 勾选一条数据
      if (this.multipleSelection.length === 1) {
        // 录取状态为“已录取/未录取/放弃”--“请选择未评的数据”
        if (this.multipleSelection[0].participantPass !== 0) {
          this.$message.error(this.$t('externamExchangeBase.pleaseChooseNotComment'))
          return
        }
      }
      // 勾选多条数据
      if (this.multipleSelection.length > 1) {
        let notRated = false // 非未评
        for (let i = 0; i < this.multipleSelection.length; i++) {
          if (this.multipleSelection[i].participantPass !== 0) {
            notRated = true
          }
        }
        if (notRated) {
          this.$message.error(this.$t('externamExchangeBase.pleaseChooseNotComment'))
          return
        }
      }
      externalExchangeApi.lastListConfirm({
        "applyIdList": this.multipleSelection.map(value => {
          return value.applyId
        }),
        "participantPass": 2
      }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t('externamExchangeBase.notAdmissionSuccessfully'))
          this.onSubmit()
        }
      })
    },
    // 异动
    change(row) {
      // 只有先进行了录取，才可进行异动和放弃操作
      if (row.participantPass !== 1) {
        this.$message.error(this.$t('externamExchangeBase.canNotChange'))
        return
      }
      this.applyId = row.applyId
      this.dialogForm.nationId = row.changes.length > 0 ? row.changes[0].nationId.toString() : row.projectNationCode.toString()
      this.changeNation(this.dialogForm.nationId, 'change')
      this.dialogForm.collegeId = row.changes.length > 0 ? row.changes[0].collegeId : row.projectCollegeId
      this.dialogForm.projectStartTime = row.changes.length > 0 ? row.changes[0].projectStartTime : row.projectStartTime
      this.dialogForm.projectEndTime = row.changes.length > 0 ? row.changes[0].projectEndTime : row.projectEndTime
      this.dialogForm.projectSubCategoryName = row.projectSubCategoryName
      this.dialogForm.projectMngmtName = row.projectMngmtName
      this.dialogForm.projectStudyAbroadContent = row.projectStudyAbroadContent
      this.dialogForm.projectFundingPeriod = row.changes.length > 0 ? row.changes[0].projectFundingPeriod : row.projectFundingPeriod
      this.dialogVisible = true
    },
    // 放弃
    giveUp() {
      // 未勾选数据
      if (this.multipleSelection.length === 0) {
        this.$message.error(this.$t('externamExchangeBase.pleaseChoose'))
        return
      }
      // 勾选一条数据
      if (this.multipleSelection.length === 1) {
        // 录取状态为“未评/未录取/放弃”--“请选择未评的数据”
        if (this.multipleSelection[0].participantPass !== 1) {
          this.$message.error(this.$t('externamExchangeBase.pleaseChooseAdmission'))
          return
        }
      }
      // 勾选多条数据
      if (this.multipleSelection.length > 1) {
        let notAdmission = false // 非已录取
        for (let i = 0; i < this.multipleSelection.length; i++) {
          if (this.multipleSelection[i].participantPass !== 1) {
            notAdmission = true
          }
        }
        if (notAdmission) {
          this.$message.error(this.$t('externamExchangeBase.pleaseChooseAdmission'))
          return
        }
      }
      externalExchangeApi.lastListConfirm({
        "applyIdList": this.multipleSelection.map(value => {
          return value.applyId
        }),
        "participantPass": 3
      }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t('externamExchangeBase.giveUpSuccessfully'))
          this.onSubmit()
        }
      }) 
    },
    // 导出
    exportInfo() {
      if (this.dataList.length === 0) {
        this.$message.error(this.$t('externamExchangeBase.exportNoData'))
        return
      }
      this.exportResult = null;
      this.$refs.downloadRef.open();
      this.exportButton = true;
      externalExchangeApi.exportLastList({
        "applyIds": this.multipleSelection.map(value => {
          return value.applyId
        }),
        "projectSubCategory": 6,
        "studentFaculty": this.queryForm.condition.studentFaculty,
        "studentProfession": this.queryForm.condition.studentProfession,
        "projectNationCode": this.queryForm.condition.projectNationCode,
        "projectCollegeId": this.queryForm.condition.projectCollegeId,
        "projectStartTime": this.queryForm.condition.projectStartTimeStr,
        "projectEndTime": this.queryForm.condition.projectEndTimeStr,
        "studentParam": this.queryForm.condition.studentParam,
        "participantPass": this.queryForm.condition.participantPass
      }).then(res => {
        if (res.code === 200 && res.data) {
          let domIframe = document.createElement("iframe");
          domIframe.src = "/api/externalexchangeservice/pgOverseasExchangeProject/downloadFile?path=" + res.data
          domIframe.style.display = "none";
          document.body.appendChild(domIframe);
          this.$message.success(this.$t('externamExchangeBase.exportSuccessfully'));
        } else {
          this.$message.success(this.$t('externamExchangeBase.exportFailed'));
        }
        this.exportResult = '';
        this.exportButton = false;
        this.$refs.downloadRef.close();
      })
    },
    // 查看申请
    selectApply(row) {
      this.applyId = row.applyId
      this.indexKey = 2
    },
    // 关闭x事件
    beforeClose: function (done) {
      this.dialogVisible = false
      this.clearDialog()
    },
    // 确定
    confirm() {
      this.$refs['dialogForm'].validate(valid => {
        if (valid) {
          externalExchangeApi.goAbroadChange({
            "applyId": this.applyId,
            "nationId": String(this.dialogForm.nationId),
            "collegeId": Number(this.dialogForm.collegeId),
            "projectStartTime": this.dialogForm.projectStartTime,
            "projectEndTime": this.dialogForm.projectEndTime,
            "projectFundingPeriod": Number(this.dialogForm.projectFundingPeriod)
          }).then(res => {
            if (res.code === 200) {
              this.$message.success(this.$t('externamExchangeBase.changeSuccessfully'))
              this.dialogVisible = false
              this.clearDialog()
              this.onSubmit()
            }
          })
        }
      })
    },
    cancel() {
      this.dialogVisible = false
      this.clearDialog()
    },
    // 清除弹框相关数据
    clearDialog() {
      Object.keys(this.dialogForm).forEach(key => this.dialogForm[key] = '')
      this.applyId = ''
    },
  },
  watch: {
    auths: {
      handler(newVal) {
        if (newVal && Array.isArray(newVal) && newVal.length > 0) {
          newVal.some(item => {
            // 录取
            if (item.authCode === 'internCommuni-country-admission') {
              this.permitToAdmission = true;
            }
            // 不录取
            if (item.authCode === 'internCommuni-not-admission') {
              this.permitToNotAdmission = true;
            }
            // 放弃
            if (item.authCode === 'internComuni-country-give-up') {
              this.permitToGiveUp = true;
            }
            // 异动
            if (item.authCode === 'internCommuni-country-change') {
              this.permitToChange = true;
            }
            // 导出
            if (item.authCode === 'internCommuni-country-export') {
              this.permitToExport = true;
            }
            // 查看申请
            if (item.authCode === 'internCommuni-country-select-apply') {
              this.permitToSelectApply = true;
            }
          });
        }
      },
      deep: true,
      immediate: true
    }
  }
}
</script>
<style lang="scss" scoped>
  .condition-title {
    color: #2b3b4e;
    font-size: 18px;
    line-height: 24px;
    font-weight: bold;
  }
  .sub-title {
    margin: 16px 0;
    font-size: 16px;
    color: #000020;
    letter-spacing: 0;
  }
  .admission-confirm-container ::v-deep {
    .el-dialog__body {
      word-break: break-all!important;
    }
  }
</style>
